<?php
/**
 * Template Name: Add Team
 *
 */
//wp_cache_post_change( get_the_ID() );
get_header(); ?>

<div class="container">
    <h1><?php the_title() ?></h1>

    <div class="row-fluid">
        <div class="span9">

            <div class="card grey">
                <form class="form-horizontal" id="TeamForm">
                    <h2 id="team_details_title"><? echo get_field('name') ?> Details</h2>
                    <div class="control-group">
                        <label class="control-label" for="team_name">Team Name</label>
                        <div class="controls">
                            <input type="text" id="team_name" name="team_name" value="<? echo get_field('name') ?>">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="team_colour">Shirt Colour</label>
                        <div class="controls">
                            <select id="team_colour" name="team_colour">
                                <option selected="selected" value="red">Red</option>
                                <option value="blue">Blue</option>
                                <option value="white">White</option>
                                <option value="black">Black</option>
                                <option value="green">Green</option>
                                <option value="yellow">Yellow</option>
                            </select>
                        </div>
                    </div>
                    <hr>
                    <h2 id="team_name_title"><? echo get_field('name') ?> Players</h2>
                    <table class="table table-condensed table-bordered table-striped" id="playersTable">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th></th>
                            <th>Email</th>
                            <th style="width:80px;"></th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                    <span class="btn btn-primary btn-small add-player" style="font-weight: 800;margin-bottom: 0">Add Player &raquo;</span>
                    <hr>
                    <button type="submit" class="btn btn-primary btn-small" style="font-weight: 800;margin-bottom: 0">Save</button>

                </form>
            </div>
        </div>
    </div>
</div>

<?php get_footer(); ?>
<script src="<?php echo get_template_directory_uri(); ?>/js/PW.editTeam.js"></script>
<script>
    $(function () {

        $('#team_colour').val('<? echo get_field('colour') ?>');
        console.log('<? echo get_field('colour') ?>');

        var options = {
            dir:'<?php echo get_template_directory_uri(); ?>',
            team:'<?php echo get_the_ID(); ?>',
            booking:'<?php echo get_permalink($_REQUEST['booking']); ?>',
            players:[
                <?
                $players = json_decode(get_field('players'), true);
                foreach ($players as $key => $value) {
                    if ($key == 0) {
                        echo "{";
                    } else {
                        echo ",{";
                    }
                    echo "'name':'".$value['name']."',";
                    echo "'email':'".$value['email']."'}";
                }
                ?>
            ]

        };
        PW.editTeam.init(options);
    });
</script>